#include <REGX52.H>


sbit OneWire_DQ=P3^7;//总线口重命名

unsigned char OneWire_Init(void)
{
	unsigned char i=0;
	unsigned char AckBit=2;
	OneWire_DQ=1;//总线恢复
	OneWire_DQ=0;//拉下总线
	//延时500微秒
	i = 227;
	{
		while (--i);
	}
	OneWire_DQ=1;
	//延时70微秒
	i = 29;
	{
		while (--i);
	}
	AckBit=OneWire_DQ;//接收从机是否响应（）
	//延时500微秒
	i = 227;
	{
		while (--i);
	}
	return AckBit;
}

//发送1bit的内容
void OneWire_SenBit(unsigned char Bit)
{
	unsigned char i;
	OneWire_DQ=1;
	OneWire_DQ=0;
		//延时15微秒
		i = 4;
		{
			while (--i);
		}
		OneWire_DQ=Bit;//发送的内容
		//延时45微秒
		i = 20;
		{
			while (--i);
		}
		OneWire_DQ=1;
}

//接收1Bit内容
unsigned char OneWire_ReceiveBit(void)
{
	unsigned char Bit=2;
	unsigned char i=0;
	OneWire_DQ=1;
	OneWire_DQ=0;
	//延时5微秒
	i = 2;
	{
		while (--i);
	}
	OneWire_DQ=1;
	//延时5微秒
	i = 2;
	{
		while (--i);
	}
	Bit=OneWire_DQ;
	//延时50微秒
		i = 20;
		{
			while (--i);
		}
	return Bit;
}

//发送一个字节的内容
void OneWire_SenByte(unsigned char Byte)
{
	unsigned char i = 0;
	for(i = 0; i < 8 ; i++)
	{
		OneWire_SenBit(Byte&(0x01<<i));
	}
}

//接收一个字节的内容
unsigned char OneWire_ReceiveByte(void)
{
	unsigned char i = 0;
	unsigned char Byte = 0;
	for(i = 0; i < 8 ; i++)
	{
		Byte |= OneWire_ReceiveBit()<<i;
	}
	return Byte;
}

